
from django.shortcuts import render,HttpResponse,get_object_or_404
from django.http import JsonResponse
from ..models import Device,monitor

def data_list(request):

    if request.method == 'GET':
        obj = Device.objects.all()
        return render(request, "datalist.html", {"devices": obj})
    else:

        A = request.GET.get("dev1")
        obj = Device.objects.all()
        return render(request, "datalist.html", {"devices": obj})


def data_cpu(request):

    legend = ["10.55.1.200"]

    A = ["4","4","4","4","4","4"]

    B = ["9:20","9:21","9:22","9:23","9:24","9:25"]


    series_list = [
        {
            "name": '"10.55.1.200"',
            "type": 'line',
            "data": A,
            "smooth": 'true',
        },

    ]
    x_axis = B

    result = {
        "status": True,
        "data": {
            "legend": legend,
            "series_list": series_list,
            "x_axis": x_axis,
        }
    }
    return JsonResponse(result)



def data_mem(request):

    ip1 = request.GET.get("ip")

    title = str(ip1) + " 设备内存利用率"
    legend = [ip1]
    A = ["4","4","4","4","4","4"]
    B = ["9:20", "9:21", "9:22", "9:23", "9:24", "9:25"]
    x = []
    y1= []
    date1 = monitor.objects.filter(net_ip=ip1).values()
    for i in date1:
        x.append(i["cpu_info"])
        y1.append(i["time"])

    y = []
    for i in y1:
        # y.append(str(i).split(".")[0])
        y.append(str(i).split(".")[0].split(" ")[1].split(":")[0] + ":" + str(i).split(".")[0].split(" ")[1].split(":")[1])



    series_list = [
        {
            "name": ip1,
            "type": 'line',
            "data": x,
            "smooth": 'true',
        },

    ]
    y_axis = y

    result = {
        "status": True,
        "data": {
            "legend": legend,
            "series_list": series_list,
            "y_axis": y_axis,
            "title":title,
        }
    }
    return JsonResponse(result)



def data_wen(request):

    ip1 = request.GET.get("ip")
    title = str(ip1) + " 设备温度"


    date1 = monitor.objects.filter(net_ip=ip1).values().last()
    temp1 = str(date1["temp"]) + "°"

    title = str(ip1) + " 设备温度为：" + temp1
    db_data_list = [
        {"value": date1["temp"], "name": ip1},
    ]

    result = {
        "status": True,
        "data": db_data_list,
        "title": title,
    }
    return JsonResponse(result)
